package com.webcamtracker.tracker.example;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;


public class TrayIntegration {

    public static void main(String[] args) {
        final TrayIcon trayIcon;


        SystemTray tray;
        if (SystemTray.isSupported()) {

            final JFrame jframe = new JFrame("Hide Test");
            jframe.getContentPane().add(new JLabel("Test"));
            final JButton button = new JButton("Hide");
            button.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    jframe.setVisible(false);
                }
            });
            jframe.getContentPane().add(button);
            jframe.setSize(new Dimension(200,200));

            tray = SystemTray.getSystemTray();
            Image image = Toolkit.getDefaultToolkit().getImage("images/duke.gif");
            PopupMenu popup = new PopupMenu();
            MenuItem defaultItem = new MenuItem("Exit");
            ActionListener exitListener = new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    System.out.println("Exiting...");
                    System.exit(0);
                }
            };
            defaultItem.addActionListener(exitListener);
            popup.add(defaultItem);
            trayIcon = new TrayIcon(image, "Tray Demo", popup);

            MouseListener mouseListener = new MouseListener() {

                public void mouseClicked(MouseEvent e) {
                    System.out.println("Tray Icon - Mouse clicked!");
//                    trayIcon.displayMessage("Mouse Event" , "The mouse was clicked", TrayIcon.MessageType.ERROR);
                    jframe.setVisible(true);
                }

                public void mouseEntered(MouseEvent e) {
                    System.out.println("Tray Icon - Mouse entered!");
                }

                public void mouseExited(MouseEvent e) {
                    System.out.println("Tray Icon - Mouse exited!");
                }

                public void mousePressed(MouseEvent e) {
                    System.out.println("Tray Icon - Mouse pressed!");
                }

                public void mouseReleased(MouseEvent e) {
                    System.out.println("Tray Icon - Mouse released!");
                }
            };


//            ActionListener actionListener = new ActionListener() {
//                public void actionPerformed(ActionEvent e) {
//                    trayIcon.displayMessage("Action Event",
//                            "An Action Event Has Been Performed!",
//                            TrayIcon.MessageType.INFO);
//                }
//            };

            trayIcon.setImageAutoSize(true);
//            trayIcon.addActionListener(actionListener);
            trayIcon.addMouseListener(mouseListener);


            try {
                tray.add(trayIcon);
            } catch (AWTException e) {
                System.err.println("TrayIcon could not be added.");
            }

        } else {

            //  System Tray is not supported

        }
    }
}
